<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item style="margin-left: -10px" label="航班号" prop="flightNo">
        <el-input
          v-model="queryParams.flightNo"
          placeholder="请输入航班号"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 180px"
        />
      </el-form-item>
      <el-form-item label="是否中转" >
        <el-select v-model="queryIsTransfer" clearable style="width: 120px">
          <el-option
            v-for="dict in dict.type.transfer_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="flightList" >
      <el-table-column show-overflow-tooltip  label="航班号" align="center" prop="flightNo" fixed>
<!--        <template slot-scope="scope">
          <span v-if="scope.row.isTransfer == '1'" class="el-tag el-tag&#45;&#45;success el-tag&#45;&#45;medium el-tag&#45;&#45;light">{{scope.row.flightNo}}</span>
          <span v-if="scope.row.isTransfer == '0'" class="el-tag el-tag&#45;&#45;warning el-tag&#45;&#45;medium el-tag&#45;&#45;light"> {{scope.row.flightNo}}  </span>
        </template>-->
      </el-table-column>
      <el-table-column label="航空公司" align="center" prop="airlineCompany" width="120"/>
      <el-table-column label="始发地" align="center" prop="origin" width="140"/>
      <el-table-column label="目的地" align="center" prop="destination" width="140"/>
      <el-table-column label="是否中转" align="center" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.transfer_type" :value="scope.row.isTransfer"/>
        </template>
      </el-table-column>
      <el-table-column label="登机口" align="center" prop="boardingGate">
        <template slot-scope="scope">
          <dict-tag v-if="scope.row.boardingGate != '3'" :options="dict.type.boarding_gate_type" :value="scope.row.boardingGate"/>
          <span v-else>{{'--'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="行李提取" align="center" prop="baggageClaimPort">
        <template slot-scope="scope">
          <dict-tag v-if="scope.row.unloadStatus == '1'" :options="dict.type.baggage_claim_port_type" :value="scope.row.baggageClaimPort"/>
          <span v-else>{{'--'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="起飞时间" align="center" prop="takeOffTime" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.takeOffTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到达时间" align="center" prop="reachTime" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.reachTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="航班状态" align="center" prop="flightStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.flight_status" :value="scope.row.flightStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="卸货状态" align="center" prop="unloadStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.unload_status" :value="scope.row.unloadStatus"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改航班对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="航班号" prop="flightNo">
          <el-input v-model="form.flightNo" :disabled="true" style="width: 228px"/>
        </el-form-item>
        <el-form-item :label="this.tackOffLable" prop="tackOffTime">
<!--          <el-col :span="11">
            <el-date-picker :disabled="true" :picker-options="pickerDateOptions" type="date" placeholder="选择日期"
                            v-model="tackOffDate" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
          </el-col>
          <el-col class="line" :span="2">-</el-col>-->
          <el-col :span="16">
            <el-time-picker format="HH:mm" :picker-options="startTimeOptions" placeholder="选择起飞时间" v-model="form.tackOffTime" style="width: 100%;"></el-time-picker>
          </el-col>
        </el-form-item>
        <el-form-item :label="this.reachLable" prop="reachTime">
<!--          <el-col :span="11">
            <el-date-picker :disabled="true" :picker-options="pickerDateOptions" type="date" placeholder="选择日期"
                            v-model="reachDate" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
          </el-col>
          <el-col class="line" :span="2">-</el-col>-->
          <el-col :span="16">
            <el-time-picker format="HH:mm" :picker-options="endTimeOptions" placeholder="选择到达时间" v-model="form.reachTime" style="width: 100%;"></el-time-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="是否中转" prop="isTransfer">
          <el-select v-model="form.isTransfer" clearable style="width: 228px">
            <el-option
              v-for="dict in dict.type.transfer_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFlight, getFlight, updateFlight } from "@/api/ground/flight";

export default {
  name: "Flight",
  dicts: ['transfer_type', 'boarding_gate_type', 'baggage_claim_port_type',
  'flight_status', 'unload_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 航班表格数据
      flightList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        flightNo: '',
      },
      // 表单参数
      form: {
        isTransfer:'',
        tackOffTime:'',
        reachTime:'',
      },
      // 表单校验
      rules: {
        isTransfer: [
          { required: true, message: "是否中转不能为空", trigger: "change" }
        ],
        tackOffTime: [
          { required: true, message: "起飞时间不能为空", trigger: "blur" }
        ],
        reachTime: [
          { required: true, message: "到达时间不能为空", trigger: "blur" }
        ],
      },
      tackOffDate: new Date(),
      reachDate: new Date(),
      tackOffLable: '',
      reachLable:'',
      /*pickerDateOptions: {
        disabledDate: (time) => {
          return time.getTime() < Date.now() - 8.64e7;
        },
      },*/
      queryIsTransfer:'',
    };
  },
  created() {
    this.getList();
  },
  computed:{
    // 开始时间
    startTimeOptions() {
      return {
        selectableRange: `${this.getCurrentTime()}-${this.form.reachTime ? this.form.reachTime : '23:59:59'}`
      }
    },
    // 结束时间
    endTimeOptions() {
      return {
        selectableRange: `${this.form.tackOffTime ? this.form.tackOffTime : this.getCurrentTime()}-23:59:59}`
      }
    }
  },
  methods: {

    getCurrentTime() {
      const now = new Date();
      let hours = now.getHours();
      let minutes = now.getMinutes();
      let seconds = now.getSeconds();

      // 确保时间部分始终是两位数
      hours = hours < 10 ? '0' + hours : hours;
      minutes = minutes < 10 ? '0' + minutes : minutes;
      seconds = seconds < 10 ? '0' + seconds : seconds;

      return hours + ':' + minutes + ':' + seconds;
    },

    /** 查询航班列表 */
    getList() {
      this.loading = true;
      this.queryParams.isTransfer = this.queryIsTransfer;
      listFlight(this.queryParams).then(response => {
        this.flightList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form= {};
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryIsTransfer = '';
      this.handleQuery();
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const flightNo = row.flightNo
      getFlight(flightNo).then(response => {
        this.form.flightNo = response.data.flightNo;
        this.$set(this.form, 'isTransfer', response.data.isTransfer)
        if(this.form.isTransfer == '0'){
          this.tackOffLable = '起飞时间';
          this.reachLable = '到达时间';
        }else if(this.form.isTransfer == '1'){
          this.tackOffLable = '中转起飞时间';
          this.reachLable = '中转到达时间';
        }
        this.open = true;
        this.title = "修改航班";
      });
    },

    // 定义格式化封装函数
    formatData(timer) {
      const year = timer.getFullYear()
      const month = timer.getMonth() + 1 // 由于月份从0开始，因此需加1
      const day = timer.getDate()
      const hour = timer.getHours()
      const minute = timer.getMinutes()
      const second = timer.getSeconds()
      //return `${this.pad(year, 4)}-${this.pad(month)}-${this.pad(day)} ${this.pad(hour)}:${this.pad(minute)}:${this.pad(second)}`
      return `${this.pad(hour)}:${this.pad(minute)}`
    },

    // 定义具体处理标准
    // timeEl 传递过来具体的数值：年月日时分秒
    // total 字符串总长度 默认值为2
    // str 补充元素 默认值为"0"
    pad(timeEl, total = 2, str = '0') {
      return timeEl.toString().padStart(total, str)
    },

    /** 提交按钮 */
    submitForm() {
      if(this.form.tackOffTime != null && this.form.tackOffTime != ''){
        this.$set(this.form, "takeOffTime", this.formatData(this.form.tackOffTime))
      }
      if(this.form.reachTime != null && this.form.reachTime != ''){
        this.$set(this.form, "reachTime", this.formatData(this.form.reachTime))
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.flightNo != undefined) {
            updateFlight(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>

<style lang="scss" scoped>

</style>
